package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description DeviceDataServiceImpl
 * @Author xiao  xun
 * @Date 2024-09-13
 */
@Service
public class DeviceDataServiceImpl implements DeviceDataService {

    @Autowired
    private DeviceDataMapper deviceDataMapper;
    /**
     * 查询设备的物模型数据
     * @param deviceDataPageReqDto
     * @return
     */
    @Override
    public PageResponse<DeviceDataVo> getDeviceDataPage(DeviceDataPageReqDto deviceDataPageReqDto) {
        PageHelper.startPage(deviceDataPageReqDto.getPageNum(),deviceDataPageReqDto.getPageSize());
        Long startTime = deviceDataPageReqDto.getStartTime();
        Long endTime = deviceDataPageReqDto.getEndTime();
        //deviceDataPageReqDto.setStartDate(ObjectUtil.isEmpty(startTime) ? null : LocalDateTimeUtil.of(startTime));
        //deviceDataPageReqDto.setEndDate(ObjectUtil.isEmpty(endTime) ? null : LocalDateTimeUtil.of(endTime));

        Page<DeviceDataVo> page = deviceDataMapper.selectByPage(deviceDataPageReqDto);

        return PageResponse.of(page,DeviceDataVo.class);
    }


    /**
     * 按日查询设备数据
     * @param iotId
     * @param functionId
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {
        //将long类型的数据转换成时间格式，里面面会判空
        LocalDateTime startTimeObj = LocalDateTimeUtil.of(startTime);
        LocalDateTime endTimeObj = LocalDateTimeUtil.of(endTime);
        List<DeviceDataGraphVo> deviceDataGraphVos = deviceDataMapper.queryDeviceDataListByDay(iotId,functionId,startTimeObj,endTimeObj);
        //将集合的数据转换成map（key=dateTime value=dateValue）
        Map<String, Double> map = deviceDataGraphVos.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
        //调用DeviceDataGraphVo里面的静态方法生成24小时的集合

        List<DeviceDataGraphVo> deviceDataGraphVos24 = DeviceDataGraphVo.dayInstance(startTimeObj);
        deviceDataGraphVos24.forEach(deviceDataGraphVo -> {//遍历里面每个时间段
            Double value = map.get(deviceDataGraphVo.getDateTime());//取出
            if (value!= null){
                deviceDataGraphVo.setDataValue(value);
            }

        });
        return deviceDataGraphVos24;
    }
    /**
     * 定时清理DeviceDate中的数据
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void cleanDataTask(){
        long oneMonthInMillis = 30 * 24 * 60 * 60 * 100l;
        Date beforeDate  = new Date(System.currentTimeMillis() - oneMonthInMillis);
        //将Long类型的时间，转化成日期时间
        LocalDateTime oneMonthAgo = LocalDateTimeUtil.of(beforeDate);
        deviceDataMapper.cleanDataTask(oneMonthAgo);
    }


    /**
     * 按周查询设备数据
     * @param iotId
     * @param functionId
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {
        //将long类型的数据转换成时间格式，里面面会判空
        LocalDateTime startTimeObj = LocalDateTimeUtil.of(startTime);
        LocalDateTime endTimeObj = LocalDateTimeUtil.of(endTime);
        List<DeviceDataGraphVo> deviceDataGraphVos = deviceDataMapper.queryDeviceDataListByWeek(iotId,functionId,startTimeObj,endTimeObj);
        //将集合的数据转换成map（key=dateTime value=dateValue）
        Map<String, Double> map = deviceDataGraphVos.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
        //调用DeviceDataGraphVo里面的静态方法生成24小时的集合

        List<DeviceDataGraphVo> deviceDataGraphVosWeek = DeviceDataGraphVo.weekInstance(startTimeObj);
        deviceDataGraphVosWeek.forEach(deviceDataGraphVo -> {//遍历里面每个时间段
            Double value = map.get(deviceDataGraphVo.getDateTime());//取出
            if (value!= null){
                deviceDataGraphVo.setDataValue(value);
            }

        });
        return deviceDataGraphVosWeek;
    }
}
